/**
 * @Title: PreviewPagerAdapter.java
 * @Package: com.jason.bluetooth.fingerprint.adapter
 * @Descripton: TODO
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2014年10月22日 下午9:39:58
 * @Version: V1.0
 */
package com.jason.platform.supervision.adapter;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.hzlj.supervision.changsha.R;
import com.jason.platform.supervision.common.FileHelper;
import com.jason.platform.supervision.widght.RecyclingImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import org.kymjs.aframe.bitmap.KJBitmap;
import org.kymjs.aframe.bitmap.KJBitmapConfig;
import org.kymjs.aframe.utils.DensityUtils;

/**
 * @ClassName: PreviewPagerAdapter
 * @Description: 图片预览器适配器
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2014年10月22日 下午9:39:58
 */
public class PreviewPagerAdapter extends PagerAdapter {


	private static final String TAG = PreviewPagerAdapter.class.getSimpleName();
	
	private DisplayImageOptions mDisplayImageOptions;
	private LayoutInflater mLayoutInflater;
	private Context mContext;
	private String[] mImages;
	private KJBitmap mKjBitmap;
	private int mWidth;
	private int mHeight;

	public PreviewPagerAdapter(Context context, String[] image, DisplayImageOptions displayImageOptions){
		this.mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		this.mImages = image;
		this.mDisplayImageOptions = displayImageOptions;
		initBitmap(context);
	}

	public PreviewPagerAdapter(Context context, String[] image){
		this.mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		this.mImages = image;
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inSampleSize = 4;
		this.mDisplayImageOptions = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.ic_empty)
				.showImageForEmptyUri(R.drawable.ic_empty)
				.showImageOnFail(R.drawable.ic_error)
				.bitmapConfig(Bitmap.Config.ARGB_4444)
				.decodingOptions(options)
				.cacheOnDisc(true)
				.cacheInMemory(true)
				.build();
		initBitmap(context);
	}

	private void initBitmap(Context context){
		KJBitmapConfig config = new KJBitmapConfig();
		config.openMemoryCache = false;
		config.cachePath = new FileHelper(context).getCachesDir().getPath();
		mKjBitmap = KJBitmap.create(config);
		mWidth = DensityUtils.getScreenW((Activity)context);
		mHeight = DensityUtils.getScreenH((Activity) context);
	}

	/* (non-Javadoc)
	 * @see android.support.v4.view.PagerAdapter#getCount()
	 */
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mImages.length;
	}

	/* (non-Javadoc)
	 * @see android.support.v4.view.PagerAdapter#isViewFromObject(android.view.View, java.lang.Object)
	 */
	
//	private View imageLayout = null;
	@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
	@Override
	public View instantiateItem(ViewGroup container, int position) {

		View imageLayout = mLayoutInflater.inflate(R.layout.adapter_pager_image, container, false);
		RecyclingImageView imageView =  (RecyclingImageView) imageLayout.findViewById(R.id.image);
		ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);
		String imagePath = mImages[position];
		Log.i(TAG,"imagePath:" + imagePath);
		final ProgressBar spinners = spinner;
		ImageLoader.getInstance().displayImage(mImages[position], imageView, mDisplayImageOptions, new SimpleImageLoadingListener() {
			@Override
			public void onLoadingStarted(String imageUri, View view) {
				spinners.setVisibility(View.VISIBLE);
			}

			@Override
			public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
				String message = null;
				switch (failReason.getType()) {
					case IO_ERROR:
						message = "Input/Output error";
						break;
					case DECODING_ERROR:
						message = "Image can't be decoded";
						break;
					case NETWORK_DENIED:
						message = "Downloads are denied";
						break;
					case OUT_OF_MEMORY:
						message = "Out Of Memory error";
						break;
					case UNKNOWN:
						message = "Unknown error";
						break;
				}
				Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();

				spinners.setVisibility(View.GONE);
			}

			@Override
			public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
				spinners.setVisibility(View.GONE);
			}
		});
		try {
			((ViewPager) container).addView(imageLayout);
		} catch (Exception e) {
			// TODO: handle exception
		}
//		container.addView(imageLayout, 0);
		return imageLayout;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView((View) object);
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view == object;
	}
	
	
	class ViewHolder{
		RecyclingImageView imageView;
		ProgressBar spinner;
	}

}
